package com.ai.smlcore.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ai.smlcore.service.ICacheService;
import com.ai.smlcore.util.BusinessException;
import com.ai.smlcore.util.CacheUtil;
import com.ai.smlcore.util.JedisUtil;

import redis.clients.jedis.JedisCluster;

public class CacheServiceImpl implements ICacheService {

	@Override
	public boolean put2Cache(String cacheKey, String value) throws BusinessException {
		return CacheUtil.put2Cache(cacheKey, value);
	}

	@Override
	public boolean put2Cache(String cacheKey, String value, int seconds) throws BusinessException {
		return CacheUtil.put2Cache(cacheKey, value, seconds);
	}

	@Override
	public String getFromCache(String cacheKey) throws BusinessException {
		return CacheUtil.getFromCache(cacheKey);
	}

	@Override
	public long incr(String key) throws BusinessException {
		return CacheUtil.incr(key);
	}

	@Override
	public long decr(String key) throws BusinessException {
		JedisCluster cluster = JedisUtil.getCluster();
		long keyLong = cluster.decr(key);
		return keyLong;
	}

	@Override
	public String putMap(String key, Map<String, String> map) throws BusinessException {
		// System.out.println("putMap key="+key +"
		// value="+JsonUtil.convertObject2Json(map));
		JedisCluster cluster = JedisUtil.getCluster();
		String str = cluster.hmset(key, map);
		return str;
	}

	@Override
	public Map<String, String> getMap(String key) throws BusinessException {
		JedisCluster cluster = JedisUtil.getCluster();
		Map<String, String> map = cluster.hgetAll(key);
		return map;
	}

	@Override
	public Long lpush(String key, String... values) throws BusinessException {
		JedisCluster cluster = JedisUtil.getCluster();
		Long longValue = cluster.lpush(key, values);
		return longValue;
	}

	public Long rpush(String key, int seconds, String... values) throws BusinessException {
		JedisCluster cluster = JedisUtil.getCluster();
		Long longValue = cluster.rpush(key, values);
		cluster.expire(key, seconds);
		return longValue;
	}

	public List<String> lrange(String key, int start, int end, int seconds) throws BusinessException {
		JedisCluster cluster = JedisUtil.getCluster();
		List<String> value = cluster.lrange(key, start, end);
		cluster.expire(key, seconds);
		return value;
	}

	@Override
	public String lpop(String key) throws BusinessException {
		JedisCluster cluster = JedisUtil.getCluster();
		String str = cluster.lpop(key);
		return str;
	}

	@Override
	public String rpop(String key) throws BusinessException {
		JedisCluster cluster = JedisUtil.getCluster();
		String str = cluster.rpop(key);
		return str;
	}

	@Override
	public Long zadd(String key, double score, String member) throws BusinessException {
		JedisCluster cluster = JedisUtil.getCluster();
		Long longValue = cluster.zadd(key, score, member);
		return longValue;
	}

	@Override
	public Set<String> zrevrangeByScore(String key, double max, double min) throws BusinessException {
		JedisCluster cluster = JedisUtil.getCluster();
		Set<String> set = cluster.zrevrangeByScore(key, max, min);
		return set;
	}

	@Override
	public Long zrem(String key, String... member) throws BusinessException {
		JedisCluster cluster = JedisUtil.getCluster();
		Long longValue = cluster.zrem(key, member);
		return longValue;
	}

	@Override
	public void delFromCache(String cacheKey) throws BusinessException {
		CacheUtil.delFromCache(cacheKey);
	}

	public boolean exists(String cacheKey) throws BusinessException {
		return CacheUtil.exists(cacheKey);
	}

	public boolean setnx(String cacheKey, String value) throws BusinessException {
		return CacheUtil.setnx(cacheKey, value);
	}
}